// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy

using System;
using System.Globalization;
using EntLibContrib.Logging.Parser;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Logging.UnitTests.Parser
{
    [TestClass]
    public class TimeStampParserTests
    {
        [TestMethod]
        public void TestTimeStampParserFraction()
        {
            const string timeStampFormat = "o";
            DateTime dateTime = new DateTime(2007, 5, 30, 23, 59, 59, 999);
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                if (!ci.IsNeutralCulture)
                {
                    string timeStampString = dateTime.ToString(timeStampFormat, ci);  // TimeStampToken.FormatToken
                    TestTimeStamp(new TimeStampParserFraction().ParseTimeStamp(timeStampString));
                    Assert.IsTrue(dateTime.Hour == 23);
                    Assert.IsTrue(dateTime.Millisecond == 999);
                }
            }
        }

        [TestMethod]
        public void TestTimeStampParserFormat()
        {
            const string timeStampFormat = "u";
            DateTime dateTime = new DateTime(2007, 5, 30, 23, 59, 59);
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                if (!ci.IsNeutralCulture)
                {
                    string timeStampString = dateTime.ToString(timeStampFormat, ci);  // TimeStampToken.FormatToken
                    TestTimeStamp(new TimeStampParserFormat(timeStampFormat, ci).ParseTimeStamp(timeStampString));
                }
            }
        }

        [TestMethod]
        public void TestCultureInfo()
        {
            DateTime dateTime = new DateTime(2007, 5, 30);
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                if (!ci.IsNeutralCulture)
                {
                    string timeStampString = dateTime.ToString(ci);
                    TestTimeStamp(new TimeStampParser(ci).ParseTimeStamp(timeStampString));
                }
            }
        }

        [TestMethod]
        public void TestParseTimeStamp()
        {
            TestTimeStamp(new TimeStampParser(new CultureInfo("de-DE")).ParseTimeStamp("30.05.2007 00:01:04"));
            TestTimeStamp(new TimeStampParser(new CultureInfo("en-US")).ParseTimeStamp("05/30/2007 00:01:04"));
        }

        private void TestTimeStamp(DateTime dateTime)
        {
            Assert.IsTrue(dateTime.Month == 05);
            Assert.IsTrue(dateTime.Day == 30);
        }
    }
}
